﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.DocumentGenerator;
using MS.InformationManagement.Support;
using System.Text;
using MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.WP.DocumentCenter.DocumentGenerator
{
    public partial class DocumentGeneratorUserControl : BaseControl, IDocumentGeneratorView
    {
        #region [Private Fields]

        private DocumentGeneratorPresenter _Presenter;

        #endregion

        #region [Event Handlers]

        protected void rdbSelectedTemplate_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton selectedElement = sender as RadioButton;
            foreach (GridViewRow currentRow in grdvTemplateList.Rows) {
               TableCell targetCell =  currentRow.Cells[currentRow.Cells.Count - 1];
               RadioButton rdbTemplateSelector = targetCell.FindControl("rdbSelectedTemplate") as RadioButton;
               if (rdbTemplateSelector != null && !rdbTemplateSelector.Equals(selectedElement)) { rdbTemplateSelector.Checked = false; }
            }
            
            HiddenField selectedTemplatePath = selectedElement.Parent.FindControl("hdfTemplatePath") as HiddenField;
            SelectedTemplatePath = selectedTemplatePath.Value;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);            
            _Presenter = new DocumentGeneratorPresenter(this);
            pgrTemplates.OnPageChanged += new Pager.PageChangeHandler(pgrTemplates_OnPageChanged);
            //pgrTemplates.NextLinkText = ">>";
            //pgrTemplates.PreviousLinkText = "<<";
            pgrTemplates.NumberOfPagesToDisplay = 10;
            PageIndex = 0;
            PageSize = 10;
            _Presenter.HandleLoad();
            pgrTemplates.BuildPager();
            txtbDocName.RegularExpression(RegularExpresions.EXP_ALFANUMERIC_NO_TILDE_NUMBERS, ExceptionConstants.ERROR_INVALID_CHARACTERS, "vgCreateDocument", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
        }

        void pgrTemplates_OnPageChanged(object sender, Controls.PageChangeEventArgs e)
        {
            _Presenter.SearchTemplates();
            pgrTemplates.BuildPager();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LibraryID = Request.QueryString.Get("lstID").Replace("{", string.Empty).Replace("}", string.Empty);
                if (string.IsNullOrEmpty(LibraryID))
                {
                    HandleWarning("Por favor verifique si el usuario se encuentra creado");
                    btnImgSearch.Enabled = false;
                }
            }
        }

        protected void btnImgSearch_Click(object sender, ImageClickEventArgs e)
        {
            txtbDocName.Text = string.Empty;
            txtbDocName.Enabled = false;
            //txtRespuestaRadicado.Text = string.Empty;
            //txtRespuestaRadicado.Enabled = false;

            PageIndex = 0;
            _Presenter.SearchTemplates();
            pgrTemplates.BuildPager();
        }        

        #endregion

        #region [IDocumentGeneratorView Members]

        public System.Collections.Generic.IEnumerable<DAL.RecordCenter.TipoDocumental> Templates
        {
            get
            {
                return grdvTemplateList.DataSource as System.Collections.Generic.IEnumerable<DAL.RecordCenter.TipoDocumental>;
            }
            set
            {
                grdvTemplateList.DataSource = value;
                grdvTemplateList.DataBind();
            }
        }

        public string DocumentName
        {
            get { return txtbDocName.Text.TrimEnd().TrimStart(); }
            set { txtbDocName.Text = value; }
        }

        public string TemplateSearchCriteria
        {
            get
            {
                return txtTemplateSearchCriteria.Text;
            }
            set
            {
                txtTemplateSearchCriteria.Text = value;
            }
        }

        public string GeneratedFileName
        {
            get;
            set;
        }

        public string SelectedTemplatePath
        {
            get { return hdfSelectTemplatePath.Value; }
            set
            {
                hdfSelectTemplatePath.Value = value;
                txtbDocName.Enabled = true;
                //txtRespuestaRadicado.Enabled = true;
            }
        }

        public string LibraryID
        {
            get { return hdfLibrayID.Value; }
            set { hdfLibrayID.Value = value; }
        }

        public int PageIndex
        {
            get
            {
                return pgrTemplates.PageIndex;
            }
            set
            {
                pgrTemplates.PageIndex = value;
            }
        }

        public int PageSize
        {
            get
            {
                return pgrTemplates.PageSize;
            }
            set
            {
                pgrTemplates.PageSize = value;
            }
        }

        public int TotalRecords
        {
            get
            {
                return pgrTemplates.TotalRecords;
            }
            set
            {
                pgrTemplates.TotalRecords = value;
            }
        }

        #endregion
        
    }
}
